package com.lu.adog.demo.common.adapter
        ;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lu.adog.R;
import com.lu.adog.demo.base.BaseConfig;
import com.lu.adog.demo.main.data.model.ItemModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yue
 * @date 2017/11/16 14:36
 */

public abstract class CommonAdapter extends RecyclerView.Adapter<CommonAdapter.Holder> {
    private Context context;
    private List<ItemModel> itemModelList;

    protected abstract void initItemModeArray(Context context,List<ItemModel> itemModelList);

    protected abstract void onItemModelClick(View view, int position, int itemNameStrId);

    public CommonAdapter(Context context) {
        this.context = context;
        if (itemModelList == null) {
            itemModelList = new ArrayList<>();
        } else {
            itemModelList.clear();
        }
        initItemModeArray(context,itemModelList);
    }

    public List<ItemModel> getItemModelList() {
        if (itemModelList==null){
            itemModelList = new ArrayList<>();
        }
        return itemModelList;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_name_description, parent, false);
        return new Holder(itemView);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        final ItemModel itemModel = itemModelList.get(position);
        holder.tvDescription.setText(itemModel.itemDescription);
        holder.tvName.setText(itemModel.itemName);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemModelClick(v, position, itemModel.itemName);
            }
        });
    }

    protected void visitActivity(Class clss,int titleNameId) {
        Intent intent = new Intent(context, clss);
        intent.putExtra(BaseConfig.KEY_TITLE,titleNameId);
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return itemModelList.size();
    }


    public static class Holder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvDescription;

        public Holder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name_item);
            tvDescription = itemView.findViewById(R.id.tv_description_item);
        }
    }
}